package br.empresa.agenda.web.controller;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import br.empresa.agenda.dominio.User;

@Name("register")
public class RegisterAction 
{

   @In
   private User user;
   
   /*@PersistenceContext
   private EntityManager em;*/
   
   @In
   EntityManager entityManager;

   
   @Logger
   private static Log log;
   
   public String register()
   {
	   try {
		   
		   User user = new User();
		   user.setName("teste");
		   user.setUsername("teste");
		   user.setPassword("teste");
		   entityManager.persist(user);
		   entityManager.getTransaction().commit();
	    } finally {
	    	//entityManager.close();
	    }
	   
	    
      List existing = entityManager.createNamedQuery("allUsers").getResultList();
      log.info("Lista: " + existing.size());
      return "/registered.jspx";
    		  
      /*if ( existing.size()==0 )
      {
         em.persist(user);
         log.info("Registered new user #{user.username}");
         return "/registered.jspx";
      }
      else
      {
         FacesMessages.instance().add("User #{user.username} already exists");
         return null;
      }*/
   }

}
